package com.sugon.modules.report.service.impl;

import com.sugon.dao.MesDayproductcountDao;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.modules.analyse.model.vo.DeviceKpiStatParamVO;
import com.sugon.modules.kpi.consts.DeviceOrganizationLevel;
import com.sugon.modules.kpi.model.dto.MonthlyProductCountStat;
import com.sugon.modules.report.service.IDayProductCountManager;
import com.sugon.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： YuXD
 * @description：工厂产品日产量Manager实现类, 包括产品总数和不良品等
 * @date ： 2020-11-16 下午 7:21
 * @version: 1.0
 */
@Service
public class DayProductCountManagerImpl implements IDayProductCountManager {

    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private MesDayproductcountDao mesDayproductcountDao;

    /**
     * 获取月度产品计数统计
     */
    @Override
    public Map<Integer, Integer> getMonthlyProductCountStat(DeviceKpiStatParamVO kpiStatParam) {
        List<MonthlyProductCountStat> mttrRootData;
        if (kpiStatParam.getLevel().equals(DeviceOrganizationLevel.LINE)) {
            mttrRootData = mesDayproductcountDao.monthlyProductCountStat(kpiStatParam.getYearly(), kpiStatParam.getDeviceOrganizationCode());
        } else {
            DeviceInfoEntity deviceInfo = deviceInfoService.getInfoByDevCode(kpiStatParam.getDeviceOrganizationCode());
            Assert.notNull(deviceInfo, "设备[" + kpiStatParam.getDeviceOrganizationCode() + "]信息不存在！");
            mttrRootData = mesDayproductcountDao.monthlyProductCountStat(kpiStatParam.getYearly(), deviceInfo.getDevProductCode());
        }
        if (mttrRootData == null) {
            mttrRootData = new ArrayList<>();
        }
        return mttrRootData.stream().collect(Collectors.toMap(MonthlyProductCountStat::getMonth, MonthlyProductCountStat::getTotal));
    }

    /**
     * getMonthlyBadProductCountStat
     *
     * @param kpiStatParam
     * @return
     */
    @Override
    public Map<Integer, Integer> getMonthlyBadProductCountStat(DeviceKpiStatParamVO kpiStatParam) {
        List<MonthlyProductCountStat> mttrRootData;
        if (kpiStatParam.getLevel().equals(DeviceOrganizationLevel.LINE)) {
            mttrRootData = mesDayproductcountDao.monthlyBadProductCountStat(kpiStatParam.getYearly(), kpiStatParam.getDeviceOrganizationCode());
        } else {
            DeviceInfoEntity deviceInfo = deviceInfoService.getInfoByDevCode(kpiStatParam.getDeviceOrganizationCode());
            Assert.notNull(deviceInfo, "设备[" + kpiStatParam.getDeviceOrganizationCode() + "]信息不存在！");
            mttrRootData = mesDayproductcountDao.monthlyBadProductCountStat(kpiStatParam.getYearly(), deviceInfo.getDevProductCode());
        }
        if (mttrRootData == null) {
            mttrRootData = new ArrayList<>();
        }
        return mttrRootData.stream().collect(Collectors.toMap(MonthlyProductCountStat::getMonth, MonthlyProductCountStat::getTotal));
    }

}
